home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / ltmf_120.lzh / LTMF_UTL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-26  |  7.9 KB  |  382 lines

  1. /* ------------------------------------------------------------------------- */
  2. /* ----- Let 'em Fly!   V 1.2 ------------ (c) 1991-93 by Oliver Scheel ---- */
  3. /* ------------------------------------------------------------------------- */
  4. /* ----- Module: ltmf_utl.c   Utilities ------------------------------------ */
  5. /* ------------------------------------------------------------------------- */
  6.  
  7. #include <stdio.h>
  8. #include <macros.h>
  9. #include <tos.h>
  10. #include <smallaes.h>
  11. #include <vdi.h>
  12. #include <magx_old.h>
  13. #include <portab.h>
  14.  
  15. #include "ltmf_str.h"
  16. #include "ltmf_def.h"
  17.  
  18. /* ------------------------------------------------------------------------- */
  19.  
  20. EXTERN    WORD    global[];
  21. EXTERN    MAGX_COOKIE    *magx;
  22.  
  23. /* ------------------------------------------------------------------------- */
  24.  
  25. WORD    vwk_handle = 0,
  26.     planes,
  27.     colors,
  28.     sysfont;
  29.  
  30. WORD    line_width,
  31.     line_color,
  32.     txt_height,
  33.     txt_color,
  34.     alig_vert,
  35.     alig_hor,
  36.     write_mode;
  37.  
  38. /* WORD    work_in[11] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 };
  39. */
  40. WORD    work_out[57];
  41.  
  42. WORD    gl_wchar,
  43.     gl_hchar,
  44.     gl_wbox,
  45.     gl_hbox;
  46.  
  47. WORD    act_mode = 0,
  48.     ind_mode = 0;
  49.  
  50. GRECT    wi_max;
  51.  
  52. LONG    oldstack;
  53.  
  54. /* ------------------------------------------------------------------------- */
  55.  
  56. CHAR *strrev(CHAR *s);
  57.  
  58. /* ------------------------------------------------------------------------- */
  59. /* ----- utilities --------------------------------------------------------- */
  60. /* ------------------------------------------------------------------------- */
  61.  
  62. WORD isupper(WORD ch)
  63. {
  64.     return((ch >= 'A') && (ch <= 'Z'));
  65. }
  66.  
  67. WORD islower(WORD ch)
  68. {
  69.     return((ch >= 'a') && (ch <= 'z'));
  70. }
  71.  
  72. WORD isdigit(WORD ch)
  73. {
  74.     return((ch >= '0') && (ch <= '9'));
  75. }
  76.  
  77. WORD isalpha(WORD ch)
  78. {
  79.     return(isupper(ch) || islower(ch));
  80. }
  81.  
  82. WORD isalnum(WORD ch)
  83. {
  84.     return(isalpha(ch) || isdigit(ch));
  85. }
  86.  
  87. WORD isfname(WORD ch)
  88. {
  89.     return(isalnum(ch) || ((ch >= '!') && (ch <= '-'))
  90.             || ((ch >= ':') && (ch <= '?'))
  91.             || (ch == '[') || (ch == '@')
  92.             || ((ch >= ']') && (ch <= '`'))
  93.             || ((ch >= '{') && (ch <= '~')));
  94. }
  95.  
  96. WORD ispname(WORD ch)
  97. {
  98.     return(isfname(ch) || (ch == '\\') || (ch == '.'));
  99. }
  100.  
  101. WORD isedctrl(WORD ch)
  102. {
  103.     return((ch == 27) || (ch == 8) || (ch == 127) || (ch == 0));
  104. }
  105.  
  106. WORD toupper(WORD ch)
  107. {
  108.     return(ch - (islower(ch) ? 'a' - 'A' : 0));
  109. }
  110.  
  111. WORD tolower(WORD ch)
  112. {
  113.     return(ch + (isupper(ch) ? 'a' - 'A' : 0));
  114. }
  115.  
  116. WORD stricmp(CHAR *s1, CHAR *s2)
  117. {
  118.     REG CHAR    c1, c2;
  119.  
  120.     while((c1 = tolower(*s1++)) == (c2 = tolower(*s2++)))
  121.     {
  122.         if(c1 == '\0')
  123.             return(0);
  124.     }
  125.     return(c1 - c2);
  126. }
  127.  
  128. WORD strnicmp(CHAR *s1, CHAR *s2, WORD len)
  129. {
  130.     REG CHAR    c1, c2;
  131.  
  132.     while((c1 = tolower(*s1++)) == (c2 = tolower(*s2++)))
  133.     {
  134.         if((c1 == '\0') || !(--len))
  135.             return(0);
  136.     }
  137.     return(c1 - c2);
  138. }
  139.  
  140. CHAR    _numstr[] = { "0123456789ABCDEF" };
  141.  
  142. CHAR *itoa(WORD n, CHAR *buf, WORD radix)
  143. {
  144.     REG CHAR    *p1,
  145.             *p2;
  146.  
  147.     p1 = buf;
  148.     if(n < 0)
  149.     {
  150.         *p1++ = '-';
  151.         n = -n;
  152.     }
  153.     p2 = p1;
  154.     do
  155.     {
  156.         *p1++ = _numstr[n % radix];
  157.     }
  158.     while((n /= radix) > 0);
  159.     *p1 = '\0';
  160.     strrev(p2);
  161.     return(buf);
  162. }
  163.  
  164. /* ------------------------------------------------------------------------- */
  165.  
  166. VOID go_super(VOID)
  167. {
  168.     oldstack = Super( (VOID *) 1L );        /* get current  mode */
  169.     if ( oldstack == 0L )
  170.     {
  171.         oldstack = Super( 0L );
  172.     }
  173. }
  174.  
  175. VOID go_user(VOID)
  176. {
  177.     if ( oldstack != -1L )
  178.     {
  179.         Super( (VOID *) oldstack );
  180.     }
  181. }
  182.  
  183. /* ------------------------------------------------------------------------- */
  184. /* ----- get_cookie -------------------------------------------------------- */
  185. /* ------------------------------------------------------------------------- */
  186.  
  187. LONG *get_cookie(LONG cookie)
  188. {
  189.     COOKJAR    *cookiejar;
  190.     WORD    i = 0;
  191.  
  192.     go_super();
  193.     cookiejar = *((COOKJAR **)0x05a0l);
  194.     go_user();
  195.     if(cookiejar)
  196.     {
  197.         while(cookiejar[i].id)
  198.         {
  199.             if(cookiejar[i].id == cookie)
  200.                 return(cookiejar[i].ptr);
  201.             i++;
  202.         }
  203.     }
  204.     return(0l);
  205. }
  206.  
  207. /* ------------------------------------------------------------------------- */
  208. /* ----- GEM Utilities ----------------------------------------------------- */
  209. /* ------------------------------------------------------------------------- */
  210.  
  211. /* ----- workstation handler ----------------------------------------------- */
  212.  
  213. WORD vwk_init(VOID)
  214. {
  215.     WORD    handle;
  216.     WORD    attrib[10];
  217.     WORD    d;
  218.  
  219.     handle = graf_handle(&gl_wchar, &gl_hchar, &gl_wbox, &gl_hbox);
  220. /*    v_opnvwk(work_in, &handle, work_out);
  221. */    vq_extnd(handle, 1, work_out);
  222.     planes = work_out[4];
  223.     vq_extnd(handle, 0, work_out);
  224.     colors = work_out[13];
  225.     vqt_attributes(handle, attrib);
  226.     if(AESVersion() >= 0x0400)
  227.     {
  228.         appl_getinfo(0, &sysfont, &d, &d, &d);
  229.     }
  230.     else
  231.     {
  232.         switch(gl_hchar)
  233.         {
  234.             case 16 :
  235.                 sysfont = 13;
  236.                 break;
  237.             case 32 :
  238.                 sysfont = 26;
  239.                 break;
  240.             default :
  241.                 sysfont = 6;
  242.                 break;
  243.         }
  244.     }
  245. /*    sysfont = (gl_hchar == 16) ? 13 : 6;
  246. */    txt_height = attrib[7];
  247.     txt_color = attrib[1];
  248.     alig_hor = attrib[3];
  249.     alig_vert = attrib[4];
  250.     vql_attributes(handle, attrib);
  251.     line_color = attrib[1];
  252.     line_width = attrib[3];
  253.     write_mode = attrib[2];
  254.     if((AESVersion() >= 0x0340) && !magx)
  255.     {
  256.         objc_sysvar(0, 1, 0, 0, &ind_mode, &d);
  257.         objc_sysvar(0, 2, 0, 0, &act_mode, &d);
  258.     }
  259.     return(handle);
  260. }
  261.  
  262. VOID vwk_exit(WORD handle)
  263. {
  264.     WORD    d;
  265.  
  266.     vst_alignment(handle, alig_hor, alig_vert, &d, &d);
  267.     vst_height(handle, txt_height, &d, &d, &d, &d);
  268.     vst_color(handle, txt_color);
  269.     vswr_mode(handle, write_mode);
  270.     vsl_color(handle, line_color);
  271.     vsl_width(handle, line_width);
  272. }
  273.  
  274. /* ----- object x/y/w/h ---------------------------------------------------- */
  275.  
  276. VOID obj_xywh(OBJECT *tree, WORD obj, GRECT *p)
  277. {
  278.     objc_offset(tree, obj, &p->g_x, &p->g_y);
  279.     p->g_w = tree[obj].ob_width;
  280.     p->g_h = tree[obj].ob_height;
  281. }
  282.  
  283. /* ----- object clipping size ---------------------------------------------- */
  284.  
  285. VOID STDARGS obj_clsize(OBJECT *tree, WORD obj, WORD *x, WORD *y, WORD *w, WORD *h)
  286. {
  287.     WORD    outl = 0,
  288.         shad = 0;
  289.     OBJECT    *tr;
  290.  
  291.     objc_offset(tree, obj, x, y);
  292.     tr = &tree[obj];
  293.     switch(tr->ob_type & 0xff)
  294.     {
  295.         case G_BUTTON :    outl--;
  296.                 if(tr->ob_flags & EXIT)
  297.                 {
  298.                     outl--;
  299.                     if(tr->ob_flags & DEFAULT)
  300.                         outl--;
  301.                 }
  302.                 break;
  303.         case G_BOX    :
  304.         case G_IBOX    :
  305.         case G_BOXCHAR    : outl = tr->ob_spec.obspec.framesize;
  306.                   break;
  307.         case G_BOXTEXT    :
  308.         case G_FBOXTEXT    : outl = tr->ob_spec.tedinfo->te_thickness;
  309.     }
  310.     if(tr->ob_state & OUTLINED)
  311.     {
  312.         if(outl > -3)
  313.             outl = -3;
  314.     }
  315.     if(tr->ob_state & SHADOWED)
  316.         shad = 2 * ((outl < 0) ? -outl : outl);
  317.     if(outl >= 0)
  318.         outl = 0;
  319.  
  320.     if((AESVersion() >= 0x0400) && (tr->ob_flags & FL3DIND))
  321.         outl -= 2;
  322.  
  323.     *x += outl;
  324.     *y += outl;
  325.     *w = tr->ob_width + shad - outl * 2;
  326.     *h = tr->ob_height + shad - outl * 2;
  327. }
  328.  
  329. /* ----- update object ----------------------------------------------------- */
  330.  
  331. VOID obj_update(OBJECT *tree, WORD obj)
  332. {
  333.     GRECT    p;
  334.  
  335.     obj_xywh(tree, obj, &p);
  336.     objc_draw(tree, obj, MAX_DEPTH, p.g_x, p.g_y, p.g_w, p.g_h);
  337. }
  338.  
  339. /* ----- copy rectangle ---------------------------------------------------- */
  340.  
  341. VOID rc_copy(GRECT *src, GRECT *dest)
  342. {
  343.     dest->g_x = src->g_x;
  344.     dest->g_y = src->g_y;
  345.     dest->g_w = src->g_w;
  346.     dest->g_h = src->g_h;
  347. }
  348.  
  349. /* ----- intersection of two rectangles ------------------------------------ */
  350.  
  351. WORD rc_intersect(VRECT *v1, VRECT *v2)
  352. {
  353.     WORD    x, y, w, h;
  354.  
  355.     x = max(v2->v_x1, v1->v_x1);
  356.     y = max(v2->v_y1, v1->v_y1);
  357.     w = min(v2->v_x2, v1->v_x2);
  358.     h = min(v2->v_y2, v1->v_y2);
  359.  
  360.     return((w >= x) && (h >= y));
  361. }
  362.  
  363. /* ----- convert VRECT to GRECT -------------------------------------------- */
  364.  
  365. VOID rc_vtog(VRECT *src, GRECT *dest)
  366. {
  367.     dest->g_x = src->v_x1;
  368.     dest->g_y = src->v_y1;
  369.     dest->g_w = src->v_x2 - src->v_x1 + 1;
  370.     dest->g_h = src->v_y2 - src->v_y1 + 1;
  371. }
  372.  
  373. /* ----- convert GRECT to VRECT -------------------------------------------- */
  374.  
  375. VOID rc_gtov(GRECT *src, VRECT *dest)
  376. {
  377.     dest->v_x1 = src->g_x;
  378.     dest->v_y1 = src->g_y;
  379.     dest->v_x2 = src->g_x + src->g_w - 1;
  380.     dest->v_y2 = src->g_y + src->g_h - 1;
  381. }
  382.